package isv

import (
    "fmt"
    "dhfshop/models/isv"
	"dhfshop/controllers/base"
	"github.com/astaxie/beego/validation"
	"wxsdk/miniapp/wxapp"
)

type AnalysisController struct {
	base.BaseController
}

// 
func (this *AnalysisController) AnalysisDataGet() {
    v := struct {
	    AuthorizerAppid string  `form:"authorizer_appid" valid:"Required"`
		ApiType         string  `form:"api_type" valid:"Required"`
		BeginDate       string  `form:"begin_date" valid:"Required"`
		EndDate         string  `form:"end_date" valid:"Required"`
	}{} 
	this.ParseForm(&v)
	valid := validation.Validation{}
	passed, _ := valid.Valid(&v)
	if !passed {
		this.ServeError(valid.Errors[0].Key+" "+valid.Errors[0].Message)
		return 
	}
	custAccount, err := isv.GetCustAccountByAppId(v.AuthorizerAppid)
	if err != nil {
	    this.ServeError(err.Error())
		return
	}	
	reqUrl := fmt.Sprintf("https://api.weixin.qq.com/datacube/getweanalysisappid%s", v.ApiType)
	data, err := wxapp.GetDataAnalysis(reqUrl,v.BeginDate,v.EndDate,custAccount.AccessToken)
	if err != nil {
	    this.ServeError(err.Error())
		return
	}
	this.Ctx.ResponseWriter.Write([]byte(data))
}
